package entity

import (
	"shqsoft.cn/pasnj/base"
	//"strconv"
	"time"

	"github.com/jinzhu/gorm"
)

//对私贷款批量认定
type IndiLoanDiviPl struct {
	ID          int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	SFZH        string     `gorm:"column:SFZH"`            //借据号
	KHMC        string     `gorm:"column:KHMC"`            //客户姓名
	KHJL1       string     `gorm:"column:KHJL1"`           //客户经理1
	ZB1         int        `gorm:"column:ZB1"`             //占比1
	KHJL2       string     `gorm:"column:KHJL2"`           //客户经理2
	ZB2         *int       `gorm:"column:ZB2"`             //占比2
	KHJL3       string     `gorm:"column:KHJL3"`           //客户经理3
	ZB3         *int       `gorm:"column:ZB3";`            //占比3
	KHJL4       string     `gorm:"column:KHJL4"`           //客户经理4
	ZB4         *int       `gorm:"column:ZB4";`            //占比4
	KHJL5       string     `gorm:"column:KHJL5"`           //客户经理5
	ZB5         *int       `gorm:"column:ZB5";`            //占比5
	CLJG        string     `gorm:"column:CLJG"`            //导入结果
	KSSJ        *time.Time `gorm:"column:KSSJ";type:date;` //分户日期
	ZJHM        string     `gorm:"column:ZJHM"`            //证件号码
	ISTZ        string     `gorm:"column:ISTZ"`            //批量认定处理方式：0 处理未分配 1调整已分配
	ISTZCaption string     `gorm:"-"`
}

//TableName 对私贷款认定分户情况
func (IndiLoanDiviPl) TableName() string {
	return "T_INDI_ACCO_PL_DTIP"
}

func (pl *IndiLoanDiviPl) AfterFind() (err error) {
	if pl.ISTZ == "0" {
		pl.ISTZCaption = "批量处理未分配的个贷客户"
	} else if pl.ISTZ == "1" {
		pl.ISTZCaption = "批量调整已分配的个贷客户"
	}
	return
}

//导入时验证导入的占比是否为100%
// func AfterValidation(index int, rowdatas []string) []base.ErrorData {
// 	var ZB1 float64
// 	var ZB2 float64
// 	var ZB3 float64
// 	ZB1, _ = strconv.ParseFloat(rowdatas[4], 64)
// 	if rowdatas[5] == "" {
// 		ZB2 = 0
// 	} else {
// 		ZB2, _ = strconv.ParseFloat(rowdatas[6], 64)
// 	}
// 	if rowdatas[7] == "" {
// 		ZB3 = 0
// 	} else {
// 		ZB3, _ = strconv.ParseFloat(rowdatas[8], 64)
// 	}
// 	ZB1 += ZB2 + ZB3

// 	sql := "SELECT COUNT(*) ID FROM T_INDI_LOAN WHERE EVNO=?"
// 	rows, _ := base.DB.Raw(sql, rowdatas[1]).Rows()
// 	var CUID int
// 	if rows.Next() {
// 		rows.Scan(&CUID)
// 	}

// 	errInfos := make([]base.ErrorInfo, 0, 100)
// 	errDatas := make([]base.ErrorData, 0, 100)

// 	if CUID == 0 {
// 		errinfo := base.ErrorInfo{
// 			ColIndex: 1,
// 			Message:  "借据编号错误或不存在！",
// 		}
// 		errInfos = append(errInfos, errinfo)
// 		errData := base.ErrorData{
// 			RowIndex:   index,
// 			DataValues: rowdatas,
// 			ErrorInfos: errInfos,
// 		}
// 		errDatas = append(errDatas, errData)
// 		return errDatas
// 	}
// 	return nil
// }

//IndiLoanAfterImport 导入后执行分户处理
func IndiLoanAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	ISTZ := params["ISTZ"]
	if ISTZ == "0" {
		tx.Exec("exec UP_INDI_LOAN_PL_CL")
	} else if ISTZ == "1" {
		tx.Exec("exec UP_INDI_LOAN_PL_TZ")
	}
	return nil
}

func IndiLoanDiviPlEF() base.EntityFunc {
	return func() interface{} {
		entity := IndiLoanDiviPl{}
		return &entity
	}
}
func IndiLoanDiviPlESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []IndiLoanDiviPl
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("IndiLoanDiviPl", base.NewDefaultDao(IndiLoanDiviPlEF(), IndiLoanDiviPlESF(), nil))
	// base.SetValidationCallBack("IndiLoanDiviPl", AfterValidation)
	base.SetAfterCallBack("IndiLoanDiviPl", IndiLoanAfterImport)
}
